package com.kgcx.flight.service.Impl;

import com.kgcx.common.bean.ReturnData;
import com.kgcx.common.constant.ResultCode;
import com.kgcx.common.utils.http.HttpUtils;
import com.kgcx.core.utils.HelpUtil;
import com.kgcx.flight.service.IFlightService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.SortedMap;
import java.util.TreeMap;

@Service
public class AvatarFlightService implements IFlightService {
    private static final Logger LOG = LoggerFactory.getLogger(AvatarFlightService.class);

    @Value("${avatar_flight_api_name}")
    private String flight_api_name;
    @Value("${avatar_flight_api_query}")
    private String flight_api_query;
    @Value("${avatar_flight_app_key}")
    private String flight_app_key;
    @Override
    public ReturnData<String> QueryFlight(String fnum, String date) {
        ReturnData<String> rtn = new ReturnData<>();
        SortedMap<String, Object> params = new TreeMap<String, Object>();
        params.put("key", flight_app_key);
        params.put("fnum", fnum);
        params.put("date", date);
        for (String key : params.keySet()) {
            System.out.println(key + " : " + params.get(key));
        }
//        String token = HelpUtil.MD5(HelpUtil.MD5(HelpUtil.implode("&", params) + vari_flight_app_secret));

        String url = String.format(flight_api_query + HelpUtil.implode("&", params));
        System.out.println("请求的URL：" + url);
        try {
            String result = HttpUtils.getInstance().syncGetRequest(url);
            rtn.setCode(ResultCode.SUCCESS);
            rtn.setMsg(String.format("{%s}-航班查询成功",flight_api_name));
            rtn.setData(HelpUtil.unicodeToString(result));
//            JsonUtil.toObject(result, List<FlightInfo>.getClass())
        } catch (Exception ex) {
            LOG.error(String.format("{%s}-航班查询异常",flight_api_name), ex);
            rtn.setCode(ResultCode.FAIL);
            rtn.setMsg(String.format("{%s}-航班查询异常",flight_api_name));
        }
        return rtn;
    }

    @Override
    public ReturnData<String> Booking(String fnum, String date, String dep, String arr) {
        return null;
    }

    @Override
    public ReturnData<String> Listener(String content) {
        return null;
    }
}
